<?php

class IndexController extends Vnf_Controller_Action {
    /*function init() {

        $this->view->currentController = $this->_request->getControllerName();
        $this->view->objName = "OrderDetails"; // class name of Model
        $objName = $this->view->objName;
        $this->view->objModel = $obj = new $objName();

        $this->view->pk = $obj->getPrimaryKey();
    }*/

    function indexAction()
    {
        $this->setLayout('admin');

        $this->view->translate = new Zend_Translate('tmx', BASE_DIR . '/translate/admin.tmx', 'en');
        //        echo "ddd";die;
    }

    function listAction() {
        $obj = new Item();

        $page = $this->_request->getParam('page'); // get the requested page
        $limit = $this->_request->getParam('rows', 1); // get how many rows we want to have into the grid
        $sidx = $this->_request->getParam('sidx'); // get index row - i.e. user click to sort
        $sord = $this->_request->getParam('sord'); // get the direction if(!$sidx) $sidx =1;

        $count = $obj->getTotalRecords();

        $page = $page>0 ? $page:1;
        if( $count > 0 ) {
            $total_pages = ceil($count/$limit);
        } else {
            $total_pages = 0;
        }
        if ($page > $total_pages)
            $page=$total_pages;
        $start = $limit*$page - $limit; // do not put $limit*($page - 1)
        $where = null;
        $records = $obj->getRecords($where, $sidx, $limit, $start);

        $responce = array();
        $responce['page'] = $page;
        $responce['total'] = $total_pages;
        $responce['records'] = $count;

        $i=0;
        $rows = array();
        foreach($records as $row):
            $rows[$i]['id']=$row->item_id;
            $rows[$i]['cell']=array(
                $row->item_id,
                $row->item_title,
                $row->category_id,
                $row->item_image,
                $row->item_short_description,
                $row->item_description,
                $row->item_order,
                $row->item_status
            );
            $i++;
        endforeach;
        $responce['rows'] = $rows;
        echo Zend_Json::encode($responce);die;
    }
}